<template>
  <div class="product-container">
    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <div class="loading-text">Loading...</div>
    </div>

    <!-- 空状态 -->
    <div v-else-if="!hasProducts" class="empty-container">
      <div class="empty-text">No Data ...</div>
    </div>

    <div v-else class="list_content">
      <div
        class="list_item"
        v-for="product in products"
        :key="product.id"
        @click="viewProduct(product)">
        <div class="item_img_view">
          <img class="item_img" :src="product.cover" v-img-zoom />
        </div>
        <div class="img_des mt-[12px]">{{ product.title }}</div>
        <div class="price mt-[8px]">${{ product.price }}</div>
      </div>
    </div>

    <!-- 加载更多状态 -->
    <div v-if="loadMoreLoading" class="load-more-container">
      <div class="load-more-spinner">
        <svg
          class="animate-spin"
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          xmlns="http://www.w3.org/2000/svg">
          <circle
            cx="12"
            cy="12"
            r="10"
            stroke="currentColor"
            stroke-width="4"
            stroke-dasharray="31.416"
            stroke-dashoffset="31.416">
            <animate
              attributeName="stroke-dasharray"
              dur="2s"
              values="0 31.416;15.708 15.708;0 31.416"
              repeatCount="indefinite" />
            <animate
              attributeName="stroke-dashoffset"
              dur="2s"
              values="0;-15.708;-31.416"
              repeatCount="indefinite" />
          </circle>
        </svg>
        <span class="load-more-text">Loading more...</span>
      </div>
    </div>

    <!-- 没有更多数据提示 -->
    <NoMore v-else-if="!hasMore && products?.length > 0 && !loading && !loadMoreLoading" />
  </div>
</template>

<script setup lang="ts">
  import { ref, computed, onMounted, onUnmounted, nextTick, watch } from 'vue';
  import NoMore from '@/components/NoMore.vue';
  import { useRouter } from 'vue-router';
  import { PayAPI } from '@/api/pay';
  import { useOrderStore } from '@/store/order';
  import { OrderType } from '@/api/nex';
  const router = useRouter();
  const orderStore = useOrderStore();

  const props = defineProps({
    loading: { type: Boolean, default: false },
    loadMoreLoading: { type: Boolean, default: false },
    hasMore: { type: Boolean, default: false },
    products: { type: Object as () => any, default: () => [] },
  });

  // 计算属性
  const hasProducts = computed(() => props.products && props.products.length > 0);

  const viewProduct = async (product: any) => {
    const params = {
      product_id: product.product_id,
      pay_method: 1,
      order_type: OrderType.PRODUCT,
    };
    orderStore.createOrderHandle(params);
  };

  // 生命周期
  onMounted(() => {});

  onUnmounted(() => {});
</script>

<style lang="scss" scoped>
  @use '@/styles/variables' as *;

  .list_content {
    display: grid; /* 启用 Grid 布局 */
    grid-template-columns: repeat(4, 1fr); /* 4 列，每列宽度平均分配 */
    gap: 16px;
    .list_item {
      box-sizing: border-box; /* 确保边框和内边距不影响宽度计算 */
      margin-bottom: 16px;
    }
    .item_img_view {
      width: 100%;
      max-height: 400px;
      border-radius: 8px;
      overflow: hidden;
    }
    .item_img {
      width: 100%;
      height: 100%;
      border-radius: 8px;
      object-fit: cover;
    }
  }

  .product-container {
    padding: 8px 24px 24px 24px;
    position: relative;
    padding-bottom: 50px;
  }
  .img_des {
    white-space: nowrap; /* 强制文本在一行内显示，不换行 */
    overflow: hidden; /* 隐藏超出容器的内容 */
    text-overflow: ellipsis; /* 用省略号表示超出的内容 */
    width: 380px; /* 根据实际需求调整宽度 */
    color: var(---9, #222);

    font-size: 14px;
    font-style: normal;
    font-weight: 600;
    line-height: normal;
  }
  .price {
    color: var(---9, #222);

    font-size: 18px;
    font-style: normal;
    font-weight: 600;
    line-height: normal;
  }

  .masonry-container {
    position: relative;
    width: 100%;
    //min-height: 60vh;
  }

  .masonry-item {
    position: absolute;
    width: calc(25% - 12px); // 考虑16px横向间距
    overflow: hidden;
    transition: all 0.3s ease;
    cursor: pointer;
    border-radius: 8px;

    // 加载状态样式
    &.loading {
      opacity: 0.7;
      background: #f5f5f5;

      &::before {
        content: '';
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 24px;
        height: 24px;
        border: 2px solid #e0e0e0;
        border-top: 2px solid #3b82f6;
        border-radius: 50%;
        animation: spin 1s linear infinite;
        z-index: 1;
      }
    }
  }

  @keyframes spin {
    0% {
      transform: translate(-50%, -50%) rotate(0deg);
    }
    100% {
      transform: translate(-50%, -50%) rotate(360deg);
    }
  }

  .product-card {
    overflow: hidden;
    transition: all 0.3s ease;
    cursor: pointer;
  }

  .product-image {
    width: 100%;
    overflow: hidden;
    border-radius: 8px;

    img {
      width: 100%;
      height: auto;
      object-fit: cover;
      display: block;
    }
  }

  .product-info {
    padding-top: 12px;
  }

  .product-name {
    color: #222;

    font-size: 14px;
    font-style: normal;
    font-weight: 600;
    line-height: normal;
  }

  .product-description {
    color: #7a7a7a;

    font-size: 14px;
    font-style: normal;
    font-weight: 400;
    line-height: normal;
    margin-top: 4px;
  }

  .icon_huo {
    border-radius: 30px 0 0 30px;
    background: rgba(255, 255, 255, 0.4);
    backdrop-filter: blur(2px);
    display: inline-flex;
    padding: 8px 12px;
    align-items: center;
    gap: 16px;
  }

  .loading-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 200px;
  }

  .loading-text {
    font-size: 16px;
    color: #7a7a7a;
  }

  .empty-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 200px;
  }

  .empty-text {
    font-size: 16px;
    color: #7a7a7a;
  }

  .load-more-container {
    //position: static; // 跟随文档流
    display: flex;
    justify-content: center;
    padding: 24px 0;
  }

  .load-more-spinner {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 24px;
    background: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(10px);
    border-radius: 24px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    color: #666;
    font-size: 14px;

    font-weight: 500;
  }

  .load-more-text {
    white-space: nowrap;
  }

  .no-more-container {
    position: static; // 跟随文档流
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 32px 0; // 较大内边距，和内容保持距离
  }

  .no-more-text {
    padding: 12px 24px;
    background: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(10px);
    border-radius: 24px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    color: #999;
    font-size: 14px;

    font-weight: 500;
    text-align: center;
    white-space: nowrap;
  }

  // 响应式设计
  @media (max-width: 1200px) {
    .masonry-item {
      width: calc(33.333% - 11px); // 3列，考虑16px间距
    }
  }

  @media (max-width: 968px) {
    .masonry-item {
      width: calc(50% - 8px); // 2列，考虑16px间距
    }
  }

  @media (max-width: 640px) {
    .masonry-item {
      width: 100%; // 1列，无需考虑间距
    }

    .product-container {
      padding: 20px 0;
    }
  }
</style>
